package com.app.bimo.commonbusiness.config;

import android.app.Application;


import com.app.bimo.commonbusiness.base.IModuleInit;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by goldze on 2018/6/21 0021.
 * 作为组件生命周期初始化的配置类，通过反射机制，动态调用每个组件初始化逻辑
 */

public class ModuleLifecycleConfig {
    //内部类，在装载该内部类时才会去创建单例对象
    private static class SingletonHolder {
        public static ModuleLifecycleConfig instance = new ModuleLifecycleConfig();
    }
    private List<IModuleInit> mModules;
    public static ModuleLifecycleConfig getInstance() {
        return SingletonHolder.instance;
    }

    private ModuleLifecycleConfig() {

    }

    //初始化组件-靠前
    public void initModuleAhead(@Nullable Application application) {
        if(this.mModules==null){
            this.mModules = new ManifestParser(application.getApplicationContext()).parse();
        }
        for (IModuleInit init:this.mModules) {
            init.onInitAhead(application);
        }
    }

    //初始化组件-靠后
    public void initModuleLow(@Nullable Application application) {
        new Thread(){
            @Override
            public void run() {
                for (IModuleInit init:mModules) {
                    init.onInitLow(application);
                }
            }
        }.start();
    }
}
